/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bd_jugueteria;

import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Jorge
 */
public class juguetes extends javax.swing.JFrame {

    private final ventana principal;
    Connection conector;

    /**
     * Creates new form juguetes
     */
    public juguetes(ventana v, Connection c) throws SQLException {
        initComponents();

     
        conector = c;
        principal = v;

        Statement st = conector.createStatement();

        ResultSet r = st.executeQuery("Select id_juguete from juguete order by id_juguete desc limit 1");
        r.next();
        System.out.println("total aqui: " + r.getString("id_juguete"));

        MostrarJuguete(r.getString("id_juguete"));
        
        
        
        

        // MostrarJuguete("");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        Tjuguete = new javax.swing.JTable();
        jLabel6 = new javax.swing.JLabel();
        jug2 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jug3 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jug4 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jug5 = new javax.swing.JTextField();
        jLabel25 = new javax.swing.JLabel();
        jug6 = new javax.swing.JTextField();
        jLabel26 = new javax.swing.JLabel();
        jug7 = new javax.swing.JTextField();
        jButton8 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jug8 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Jugueteria...");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        Tjuguete.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(Tjuguete);

        getContentPane().add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 52, 740, 153));

        jLabel6.setFont(new java.awt.Font("Comic Sans MS", 3, 12)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("Descripcion");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(18, 240, -1, -1));

        jug2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jug2ActionPerformed(evt);
            }
        });
        getContentPane().add(jug2, new org.netbeans.lib.awtextra.AbsoluteConstraints(18, 265, 83, -1));

        jLabel7.setFont(new java.awt.Font("Comic Sans MS", 3, 12)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setText("Marca");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(121, 240, -1, -1));
        getContentPane().add(jug3, new org.netbeans.lib.awtextra.AbsoluteConstraints(119, 265, 67, -1));

        jLabel8.setFont(new java.awt.Font("Comic Sans MS", 3, 12)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(255, 255, 255));
        jLabel8.setText("Precio unitario");
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(196, 240, -1, -1));

        jug4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jug4ActionPerformed(evt);
            }
        });
        getContentPane().add(jug4, new org.netbeans.lib.awtextra.AbsoluteConstraints(196, 265, 52, -1));

        jLabel9.setFont(new java.awt.Font("Comic Sans MS", 3, 12)); // NOI18N
        jLabel9.setForeground(new java.awt.Color(255, 255, 255));
        jLabel9.setText("Area");
        getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(286, 240, 47, -1));
        getContentPane().add(jug5, new org.netbeans.lib.awtextra.AbsoluteConstraints(286, 265, 47, -1));

        jLabel25.setFont(new java.awt.Font("Comic Sans MS", 3, 12)); // NOI18N
        jLabel25.setForeground(new java.awt.Color(255, 255, 255));
        jLabel25.setText("Cantidad existente");
        getContentPane().add(jLabel25, new org.netbeans.lib.awtextra.AbsoluteConstraints(351, 240, -1, -1));
        getContentPane().add(jug6, new org.netbeans.lib.awtextra.AbsoluteConstraints(351, 265, 91, -1));

        jLabel26.setFont(new java.awt.Font("Comic Sans MS", 3, 12)); // NOI18N
        jLabel26.setForeground(new java.awt.Color(255, 255, 255));
        jLabel26.setText("Proveedor");
        getContentPane().add(jLabel26, new org.netbeans.lib.awtextra.AbsoluteConstraints(556, 240, -1, -1));
        getContentPane().add(jug7, new org.netbeans.lib.awtextra.AbsoluteConstraints(556, 265, 50, -1));

        jButton8.setFont(new java.awt.Font("Papyrus", 1, 11)); // NOI18N
        jButton8.setText("Agregar");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton8, new org.netbeans.lib.awtextra.AbsoluteConstraints(143, 307, 90, -1));

        jButton11.setFont(new java.awt.Font("Papyrus", 1, 11)); // NOI18N
        jButton11.setText("Actualizar");
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton11, new org.netbeans.lib.awtextra.AbsoluteConstraints(152, 211, -1, -1));

        jButton9.setFont(new java.awt.Font("Papyrus", 1, 11)); // NOI18N
        jButton9.setText("Eliminar");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton9, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 211, 124, -1));

        jButton10.setFont(new java.awt.Font("Papyrus", 1, 11)); // NOI18N
        jButton10.setText("Buscar");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton10, new org.netbeans.lib.awtextra.AbsoluteConstraints(48, 11, 79, -1));

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });
        getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(133, 12, 124, -1));
        getContentPane().add(jug8, new org.netbeans.lib.awtextra.AbsoluteConstraints(468, 265, 70, -1));

        jLabel1.setFont(new java.awt.Font("Comic Sans MS", 3, 12)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Cantidad Total");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(468, 240, -1, -1));

        jButton1.setFont(new java.awt.Font("Papyrus", 1, 11)); // NOI18N
        jButton1.setText("VOLVER");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(18, 307, 110, -1));

        jLabel2.setBackground(new java.awt.Color(153, 153, 255));
        jLabel2.setFont(new java.awt.Font("Papyrus", 1, 11)); // NOI18N
        jLabel2.setOpaque(true);
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 760, 360));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jug2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jug2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jug2ActionPerformed

    private void jug4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jug4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jug4ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        // TODO add your handling code here://Agregar tabla juguete

        if(jug2.getText().length()>0 &&jug3.getText().length()>0 &&jug4.getText().length()>0 &&jug5.getText().length()>0 &&
                jug6.getText().length()>0 &&jug7.getText().length()>0 &&jug8.getText().length()>0 && jButton11.getText().equals("Actualizar")){
         
            
        try {
            try (PreparedStatement insertar = conector.prepareStatement("insert into "
                    + "juguete(descripcion, marca, precio_unit, area, "
                    + "cantidad_existente,idproveedor,cantidad_total) "
                    + " values(?,?,?,?,?,?,?)")) {
                insertar.setString(1, jug2.getText());
                insertar.setString(2, jug3.getText());
                insertar.setString(3, jug4.getText());
                insertar.setString(4, jug5.getText());
                insertar.setString(5, jug6.getText());
                insertar.setString(6, jug7.getText());
                insertar.setString(7, jug8.getText());
                insertar.execute();
                insertar.close();
                JOptionPane.showMessageDialog(null, "juguete Agregado");

                Statement st = conector.createStatement();

                ResultSet r = st.executeQuery("Select id_juguete from juguete order by id_juguete desc limit 1");
                r.next();
                System.out.println("total aqui: " + r.getString("id_juguete"));

                MostrarJuguete(r.getString("id_juguete"));

                jug8.setText("");
                jug2.setText("");
                jug3.setText("");
                jug4.setText("");
                jug5.setText("");
                jug6.setText("");
                jug7.setText("");

            }
        } catch (SQLException e) {
            System.out.print(e.getMessage());
            //JOptionPane.showMessageDialog(null, "ID (" + camp1.getText() + ") en uso..");
        }
            
        }
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        // TODO add your handling code here://eliminar empleado

        /* 
         int fild = Tjuguete.getSelectedRow();
         if (fild > -1) {

         String id = String.valueOf(Tjuguete.getValueAt(fild, 0));
         obj.Elimina_Datos("juguete", "id_juguete", id);
         MostrarJuguete("");
         fild = -1;
         }*/
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        // TODO add your handling code here:
        MostrarJuguete(jTextField1.getText());
        
    }//GEN-LAST:event_jButton10ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:

        this.setVisible(false);
        principal.setVisible(true);

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed
        // TODO add your handling code here:
        int fila2 = Tjuguete.getSelectedRow();
        
        if(jButton11.getText().equals("Actualizar")){
        if (fila2 >= 0) {
            //jug1.setText(Tjuguete.getValueAt(fila2, 0).toString());
            jug2.setText(Tjuguete.getValueAt(fila2, 1).toString());
            jug3.setText(Tjuguete.getValueAt(fila2, 2).toString());
            jug4.setText(Tjuguete.getValueAt(fila2, 3).toString());
            jug5.setText(Tjuguete.getValueAt(fila2, 4).toString());
            jug6.setText(Tjuguete.getValueAt(fila2, 5).toString());
            jug7.setText(Tjuguete.getValueAt(fila2, 6).toString());
            jug8.setText(Tjuguete.getValueAt(fila2, 7).toString());
            jButton11.setText("Guardar cambios");
            
        } else {
            JOptionPane.showMessageDialog(null, "Seleccionado una fila por favor");
        }
        
            
        }
        else{
            
          jButton11.setText("Actualizar");
            
            try {

            PreparedStatement pre = conector.prepareStatement("UPDATE juguete "
                + "SET descripcion = '" + jug2.getText()+ "', marca='" + jug3.getText() + "', precio_unit='" + jug4.getText()
                + "', area='" +jug5.getText() + "', cantidad_existente='" +jug6.getText() + "',idproveedor='" + jug7.getText()
                + "',cantidad_total='"+jug8.getText()+"' WHERE id_juguete='" + Tjuguete.getValueAt(fila2, 0).toString() + "';");
            pre.executeUpdate();

            
            jug2.setText("");
            jug3.setText("");
            jug4.setText("");
            jug5.setText("");
            jug6.setText("");
            jug7.setText("");
            jug8.setText("");
         
            JOptionPane.showMessageDialog(this, "Se Actualizo Juguete con Exito");
            MostrarJuguete("");
        } catch (SQLException ex) {
            //JOptionPane.showMessageDialog(this, "Error al Corregir");
            System.out.print(ex.getMessage());
         
         }
         }
            
      
        
     
          
    }//GEN-LAST:event_jButton11ActionPerformed

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped
        // TODO add your handling code here:
        
        
        
         if (!Character.isDigit(evt.getKeyChar()) && !Character.isISOControl(evt.getKeyChar())) {
            Toolkit.getDefaultToolkit().beep();//manda un sonido de error
            evt.consume();//consume el key event. y no pasa nada al jtextfield
        }

      
         
        
        
    }//GEN-LAST:event_jTextField1KeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(juguetes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(juguetes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(juguetes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(juguetes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form 
         java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
         new juguetes().setVisible(true);
         }
         });*/
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable Tjuguete;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jug2;
    private javax.swing.JTextField jug3;
    private javax.swing.JTextField jug4;
    private javax.swing.JTextField jug5;
    private javax.swing.JTextField jug6;
    private javax.swing.JTextField jug7;
    private javax.swing.JTextField jug8;
    // End of variables declaration//GEN-END:variables

    public void MostrarJuguete(String juguete) {

        System.out.println("total: " + juguete);
        
         DefaultTableModel ModeloJuguete = new DefaultTableModel();
        ModeloJuguete.addColumn("ID juguete");
        ModeloJuguete.addColumn("Descripcion ");
        ModeloJuguete.addColumn("Marca");
        ModeloJuguete.addColumn("Precio unitario");
        ModeloJuguete.addColumn("Area");
        ModeloJuguete.addColumn("Cantidad existente");
        ModeloJuguete.addColumn("Proveedor");
        ModeloJuguete.addColumn("Cantidad total");
        Tjuguete.setModel(ModeloJuguete);
        Tjuguete.isCellEditable(0,0);
        
        
        String sql = "";
        if (juguete.equals("")) {

            sql = "SELECT * FROM juguete";
        } else {
            sql = "SELECT * FROM juguete WHERE id_juguete ='" + juguete + "'";
        }
        String[] datosjug = new String[8];
        try {
            Statement st = conector.createStatement();
            ResultSet rs = st.executeQuery(sql);

            while (rs.next()) {
                datosjug[0] = rs.getString(1);
                datosjug[1] = rs.getString(2);
                datosjug[2] = rs.getString(3);
                datosjug[3] = rs.getString(4);
                datosjug[4] = rs.getString(5);
                datosjug[5] = rs.getString(6);
                datosjug[6] = rs.getString(7);
                datosjug[7] = rs.getString(8);

                ModeloJuguete.addRow(datosjug);

            }
            Tjuguete.setModel(ModeloJuguete);
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }

}
